package com.ctsi.offlinesupport.service;

import com.ctsi.commons.util.UtilDateTime;
import com.ctsi.system.interfaces.I_Calandar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author zhangjw
 * @className TimeUtil
 * @description 及时率时间处理
 * @date 2019/4/8 2:52
 **/
@Component
public class TimeUtil {

    private static SimpleDateFormat hm = new SimpleDateFormat("HH:mm");

    private static Date lunchBreak = null;
    private static Date workGoOn = null;
    private static Date offDuty = null;
    private static Date startWork = null;

    static {
        try {
            hm.setTimeZone(TimeZone.getTimeZone("CHINA_TIME_ZONE"));

            startWork = hm.parse("08:30");
            lunchBreak = hm.parse("11:30");
            workGoOn = hm.parse("13:30");
            offDuty = hm.parse("17:30");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


    @Autowired
    private I_Calandar calandar;

//    /**
//     *
//     * @param curDate 当前时间
//     * @param minute 顺延分种数
//     * @return
//     */
//    public Long getLastHandlerTime(Date curDate,int minute) {
//        Date finalDate = calandar.getWorkDate(curDate,minute,I_Calandar.MINUTES);
//        // 判断时间边界问题
//       // String finalTime = hm.format(finalDate);
//        String finalTime = UtilDateTime.toTimeString(curDate);
//        Date finalDateTime = null;
//        try {
//            finalDateTime = hm.parse(finalTime);
//        } catch (ParseException e) {
//            throw new RuntimeException("时间解析错误");
//        }
//        if (finalDateTime.before(startWork) || finalDateTime.equals(startWork)) {
//            // 此时还没有上班
//            // 获取一天得开始时间
//            long l = UtilDateTime.getDayStart(finalDate);
//            return l+startWork.getTime()+ minute*60*1000;
//        } else if (finalDateTime.after(lunchBreak) && (finalDateTime.before(workGoOn) || finalDateTime.equals(workGoOn))) {
//            // 午休时间
//            long diff = finalDateTime.getTime() - lunchBreak.getTime();
//            long l = UtilDateTime.getDayStart(finalDate);
//            if (diff > 0 && diff < minute*60*1000) {
//                return l+workGoOn.getTime()+diff;
//            }
//            return l+workGoOn.getTime()+minute*60*1000;
//        } else if (finalDateTime.after(offDuty) || finalDate.equals(offDuty)) {
//            // 下班时间
//            long l = UtilDateTime.getDayStart(finalDate);
//            long l1 = UtilDateTime.addDaysToTimestamp(l,1);
//            long diff = finalDateTime.getTime() - offDuty.getTime();
//            if (diff > 0 && diff < minute*60*1000) {
//                // 顺延
//                return calandar.getWorkDate(new Date(l1),startWork.getTime()+diff,I_Calandar.MS).getTime();
//            }
//            return calandar.getWorkDate(new Date(l1),startWork.getTime()+minute*60*1000,I_Calandar.MS).getTime();
//        }
//        return finalDate.getTime();
//    }


    /**
     *
     * @param curDate 当前时间
     * @param minute 顺延分种数
     * @return
     */
    public Long getLastHandlerTime(Date curDate,int minute) {
        int temp = minute*60*1000;
        if (calandar.isFree(curDate)) {
            // 当前时间是假期
            long l = UtilDateTime.getDayStart(curDate); // 当天得开始时间
            Date date1 = calandar.getWorkDate(new Date(l),1,I_Calandar.DAY);
            return UtilDateTime.getDayStart(date1)+startWork.getTime()+temp;
        } else {
            // 非假期
            long cumped = curDate.getTime()+temp; // 延期minutes之后得时间
            Date cumpedDate = new Date(cumped);

            String finalTime = UtilDateTime.toTimeString(cumpedDate);
            Date finalDateTime = null;
            try {
                finalDateTime = hm.parse(finalTime);
            } catch (ParseException e) {
                throw new RuntimeException("时间解析错误");
            }
            long l = UtilDateTime.getDayStart(cumpedDate);
            if (finalDateTime.before(startWork) || finalDateTime.equals(startWork)) {
                // 此时还没有上班
                return l+ startWork.getTime() + temp;
            } else if (finalDateTime.after(lunchBreak) && (finalDateTime.before(workGoOn) || finalDateTime.equals(workGoOn)) ) {
                // 午休时间
                long diff = finalDateTime.getTime() - lunchBreak.getTime();

                if (diff < temp) {
                    // 下班之前发得
                    return l+workGoOn.getTime()+diff;
                }
                if (diff >= temp) {
                    return l+workGoOn.getTime()+temp;
                }
            } else if (  (finalDateTime.getTime() - workGoOn.getTime() < temp)  && finalDateTime.before(offDuty) && finalDateTime.after(workGoOn) ) {
                return l+workGoOn.getTime()+temp;

            } else if (finalDateTime.after(offDuty)) {

                long diff = finalDateTime.getTime() - offDuty.getTime();
                // 直接延到第二天
                Date date1 = calandar.getWorkDate(new Date(l),1,I_Calandar.DAY);
                if (diff < temp ) {
                    return UtilDateTime.getDayStart(date1) + startWork.getTime() +diff;
                }
                return UtilDateTime.getDayStart(date1) + startWork.getTime() + temp;
            }


            return cumped;
        }

    }


}
